package com.etrans.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Administrator on 2018/10/24.
 */
public class CommonUtils {

    public static String getTrace(Throwable t){
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        t.printStackTrace(printWriter);
        StringBuffer buffer = stringWriter.getBuffer();
        return buffer.toString();
    }

    public static boolean checkParamIsValid (Object o, String ... params) throws Exception {
        HashMap hashMap = JSON.parseObject(JSON.toJSONString(o), HashMap.class);
        for (String item : params) {
            if (hashMap == null || !hashMap.containsKey(item) || StringUtils.isBlank(String.valueOf(hashMap.get(item)))
                    || "null".equals(String.valueOf(hashMap.get(item)))) {
                throw new Exception("参数【" + item + "】不可为空！");
            }
        }
        return true;
    }

}
